class MarketController < ApplicationController
  def index
    #@base_materials = BaseMaterial.find(:all)
    @base_vegetables = BaseMaterial.find(:all, :conditions => "mtype='vegetable'")
    @base_meat = BaseMaterial.find(:all, :conditions => "mtype='meat'")
    @base_spices = BaseSpice.find(:all)
  end

  def buy_materials
    @material = Material.find_or_create_by_user_id_and_base_material_id(@user.id, params[:id])
    amount = @material.amount.to_i
    #TODO material.amount only increase 1
    #TODO need to spend money
    @material.amount = amount + 1
    @material.mtype = BaseMaterial.find(params[:id]).mtype   # vegetable or meat
    @material.save!
    flash[:notice] = "购买 #{@material.base_material.name} 成功"
    redirect_to market_path()
  end

  def buy_spices
    @spice = Spice.find_or_create_by_user_id_and_base_spice_id(@user.id, params[:id])
    amount = @spice.amount.to_i
    #TODO spice.amount only increase 1
    #TODO need to spend money
    @spice.amount = amount + 1
    @spice.save!
    flash[:notice] = "购买 #{@spice.base_spice.name} 成功"
    redirect_to market_path()
  end

  def sold
    
  end
end
